package com.candy.service;

import com.candy.bean.UserInfo;
import com.candy.dao.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@Service
public class MyUserDetailServiceImpl implements UserDetailsService {

    private  final UserInfoMapper userInfoMapper;

    @Autowired
    public MyUserDetailServiceImpl(UserInfoMapper userDao) {
        this.userInfoMapper = userDao;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo user = userInfoMapper.loadUserByUsername(username);
        if(ObjectUtils.isEmpty(user)) {
            throw new RuntimeException("用户不存在");
        }
        user.setRoles(userInfoMapper.getRolesByUid(user.getId()));
        return user;
    }


}
